using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.UserContrl
{
	public class ContactsViewSelector : UserControl
	{
		private const string TAG = "ContactsViewSelector";
		private static ContactsViewSelector sInstance;
		private bool mBIsFirstLoaded = true;
		private ContactsView mConsView;
		private Storyboard mSBHide;
		private static Popup sPopSelector;
		private static bool sBSelectorIsShown = false;
		private static bool mBIsSetBackKey = false;
		private static BasePage sPage;
		private static bool sBIsSetBackKey = false;
		internal ScrollViewer LayoutRoot;
		internal Paragraph paraContent;
		private bool _contentLoaded;
		public event MMUIEvent mEventGroupChoosen;
		public ContactsViewSelector()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void header_ItemClick(object sender, object object_0)
		{
			ContactsView view = this.mConsView;
			base.set_IsEnabled(false);
			if (view != null)
			{
				view.GroupChoosen(object_0 as string);
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				ContactsViewSelector.HideSelector();
				view.getContactInView();
			});
		}
		private void updateUI(ContactsView view)
		{
			if (view == null)
			{
				return;
			}
			this.mConsView = view;
			this.updateUI(view.GetSelectorGroup());
		}
		private void startHideAni()
		{
			QuadraticEase quadraticEase = new QuadraticEase();
			quadraticEase.set_EasingMode(0);
			IEasingFunction easingFunction = quadraticEase;
			this.mSBHide = new Storyboard();
			List<ContactGroupItem> allHeaders = this.getAllHeaders();
			using (List<ContactGroupItem>.Enumerator enumerator = allHeaders.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ContactGroupItem current = enumerator.get_Current();
					if (current != null)
					{
						DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
						EasingDoubleKeyFrame easingDoubleKeyFrame = new EasingDoubleKeyFrame();
						easingDoubleKeyFrame.set_KeyTime(TimeSpan.FromMilliseconds(0.0));
						easingDoubleKeyFrame.set_Value(0.0);
						easingDoubleKeyFrame.set_EasingFunction(easingFunction);
						EasingDoubleKeyFrame easingDoubleKeyFrame2 = new EasingDoubleKeyFrame();
						easingDoubleKeyFrame2.set_KeyTime(TimeSpan.FromMilliseconds(500.0));
						easingDoubleKeyFrame2.set_Value(90.0);
						easingDoubleKeyFrame2.set_EasingFunction(easingFunction);
						doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame);
						doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame2);
						Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath(PlaneProjection.RotationXProperty));
						Storyboard.SetTarget(doubleAnimationUsingKeyFrames, current.get_Projection());
						this.mSBHide.get_Children().Add(doubleAnimationUsingKeyFrames);
					}
				}
			}
			this.mSBHide.add_Completed(new EventHandler(this._swivelHide_Completed));
			this.mSBHide.Begin();
		}
		private void startShowAni()
		{
			this.getAllHeaders();
		}
		private void _swivelHide_Completed(object sender, EventArgs e)
		{
			if (this.mSBHide != null)
			{
				this.mSBHide.Stop();
			}
			this.mSBHide = null;
			if (ContactsViewSelector.sPopSelector != null)
			{
				ContactsViewSelector.sPopSelector.set_IsOpen(false);
				ContactsViewSelector.sPopSelector.set_Child(null);
			}
			MainPage.mainPageInstance.mNBackKeyRelayCount--;
			ToolFunction.ResumeRootPageAppbar();
			ContactsViewSelector.unSetBackKeyHandler();
		}
		private void clearUI()
		{
			this.mConsView = null;
			if (AppInfoHelper.Is256MbDevice())
			{
				this.setItemCnt(0);
			}
			this.startHideAni();
		}
		private void scrollToTop()
		{
			this.LayoutRoot.ScrollToVerticalOffset(0.0);
		}
		private void updateUI(IList<Group<Contact>> listCons)
		{
			if (listCons != null && listCons.get_Count() != 0)
			{
				this.setItemCnt(listCons.get_Count());
				this.setKeyToItems(listCons);
				return;
			}
		}
		private void setItemCnt(int nCnt)
		{
			if (nCnt >= 0)
			{
				if (nCnt != this.paraContent.get_Inlines().get_Count())
				{
					while (nCnt > this.paraContent.get_Inlines().get_Count())
					{
						this.paraContent.get_Inlines().Add(this.getInline());
					}
					while (nCnt < this.paraContent.get_Inlines().get_Count())
					{
						ContactGroupItem headerAt = this.getHeaderAt(this.paraContent.get_Inlines().get_Count() - 1);
						this.paraContent.get_Inlines().RemoveAt(this.paraContent.get_Inlines().get_Count() - 1);
						if (headerAt != null)
						{
							headerAt.ItemClick -= new MMStandardUIEvent(this.header_ItemClick);
						}
					}
					return;
				}
			}
		}
		private void setKeyToItems(IList<Group<Contact>> listCons)
		{
			if (listCons != null)
			{
				if (listCons.get_Count() == this.paraContent.get_Inlines().get_Count())
				{
					int num = 0;
					using (IEnumerator<Group<Contact>> enumerator = listCons.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Group<Contact> current = enumerator.get_Current();
							if (current != null)
							{
								ContactGroupItem headerAt = this.getHeaderAt(num++);
								if (headerAt != null)
								{
									headerAt.set_DataContext(current);
									headerAt.mStrKey = current.Key;
								}
							}
						}
					}
					return;
				}
			}
		}
		private ContactGroupItem getHeaderAt(int nIndex)
		{
			if (nIndex < 0 || nIndex >= this.paraContent.get_Inlines().get_Count())
			{
				return null;
			}
			InlineUIContainer inlineUIContainer = this.paraContent.get_Inlines().get_Item(nIndex) as InlineUIContainer;
			if (inlineUIContainer == null)
			{
				return null;
			}
			return inlineUIContainer.get_Child() as ContactGroupItem;
		}
		private List<ContactGroupItem> getAllHeaders()
		{
			List<ContactGroupItem> list = new List<ContactGroupItem>();
			using (IEnumerator<Inline> enumerator = this.paraContent.get_Inlines().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Inline current = enumerator.get_Current();
					InlineUIContainer inlineUIContainer = current as InlineUIContainer;
					if (inlineUIContainer != null)
					{
						ContactGroupItem contactGroupItem = inlineUIContainer.get_Child() as ContactGroupItem;
						if (contactGroupItem != null)
						{
							list.Add(contactGroupItem);
						}
					}
				}
			}
			return list;
		}
		private InlineUIContainer getInline()
		{
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			ContactGroupItem contactGroupItem = new ContactGroupItem();
			contactGroupItem.ItemClick += new MMStandardUIEvent(this.header_ItemClick);
			inlineUIContainer.set_Child(contactGroupItem);
			return inlineUIContainer;
		}
		public static ContactsViewSelector GetInstance(bool isCreateNew = false)
		{
			if (ContactsViewSelector.sInstance == null && isCreateNew)
			{
				ContactsViewSelector.sInstance = new ContactsViewSelector();
			}
			return ContactsViewSelector.sInstance;
		}
		public static void ShowSelector(ContactsView view)
		{
			if (ContactsViewSelector.sBSelectorIsShown)
			{
				return;
			}
			if (ContactsViewSelector.sPopSelector == null)
			{
				ContactsViewSelector.sPopSelector = new Popup();
			}
			ContactsViewSelector.sBSelectorIsShown = true;
			ContactsViewSelector.setBackKeyHandler();
			ContactsViewSelector instance = ContactsViewSelector.GetInstance(true);
			instance.set_IsEnabled(true);
			instance.scrollToTop();
			instance.updateUI(view);
			ContactsViewSelector.sPopSelector.set_Child(instance);
			ContactsViewSelector.sPopSelector.set_IsOpen(true);
			MainPage.mainPageInstance.mNBackKeyRelayCount++;
			ToolFunction.HideRootPageAppbar();
		}
		public static void HideSelector()
		{
			if (!ContactsViewSelector.sBSelectorIsShown)
			{
				return;
			}
			ContactsViewSelector.sBSelectorIsShown = false;
			if (ContactsViewSelector.sPopSelector == null)
			{
				return;
			}
			ContactsViewSelector instance = ContactsViewSelector.GetInstance(false);
			if (instance != null)
			{
				instance.clearUI();
			}
		}
		private static void setBackKeyHandler()
		{
			if (ContactsViewSelector.mBIsSetBackKey)
			{
				return;
			}
			BasePage rootPage = ToolFunction.GetRootPage();
			if (rootPage != null)
			{
				ContactsViewSelector.sPage = rootPage;
				ContactsViewSelector.sPage.add_BackKeyPress(new EventHandler<CancelEventArgs>(ContactsViewSelector.sPage_BackKeyPress));
				ContactsViewSelector.sBIsSetBackKey = true;
			}
		}
		private static void unSetBackKeyHandler()
		{
			if (ContactsViewSelector.sBIsSetBackKey && ContactsViewSelector.sPage != null)
			{
				ContactsViewSelector.sPage.remove_BackKeyPress(new EventHandler<CancelEventArgs>(ContactsViewSelector.sPage_BackKeyPress));
				ContactsViewSelector.sPage = null;
				ContactsViewSelector.sBIsSetBackKey = false;
			}
		}
		private static void sPage_BackKeyPress(object sender, CancelEventArgs e)
		{
			ContactsViewSelector.unSetBackKeyHandler();
			e.set_Cancel(true);
			ContactsViewSelector.HideSelector();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Contact/ContactsViewSelector.xaml", 2));
			this.LayoutRoot = (ScrollViewer)base.FindName("LayoutRoot");
			this.paraContent = (Paragraph)base.FindName("paraContent");
		}
	}
}
